package practice.MyProject.view;

import practice.MyProject.bean.Custom;
import practice.MyProject.service.CustomList;
import practice.MyProject.util.CustomUtil;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Author juzipi
 * @Date 2021/6/25 10:13
 * @Info
 */
public class CustomView {

    private final CustomList customList = new CustomList(10);

    private static final Scanner scanner = new Scanner(System.in);

    private static int id = 1;

    private void enterMainMenu() {
        boolean flag = true;
        while (flag) {
            System.out.println("    -------------------     管理     -------------------     ");
            System.out.println("1.添加客户");
            System.out.println("2.修改客户");
            System.out.println("3.删除客户");
            System.out.println("4.查询所有客户");
            System.out.println("5.查询单个客户");
            System.out.println("6.退出");
            System.out.println("请选择：");
            char c = CustomUtil.readMenuSelection();
            switch (c) {
                case '1':
                    //添加
                    System.out.println("请输入姓名: ");
                    String name = scanner.nextLine();
                    System.out.println("请输入姓别: ");
                    String gender = scanner.nextLine();
                    System.out.println("请输入年龄: ");
                    Integer age = scanner.nextInt();

                    System.out.println("请输入电话: ");
                    String phoneNumber = scanner.nextLine();
                    System.out.println("请输入邮箱: ");
                    String email = scanner.next();
                    Custom custom = new Custom(id++, name, gender, age, phoneNumber, email);
                    addNewCustom(custom);
                    break;
                case '2':
                    //修改
                    System.out.println("请输入id");
                    int i1 = scanner.nextInt();
                    Custom custom2 = customList.findCustom(i1);
                    System.out.println("-----");
                    System.out.println("姓名：");
                    String modifyName = scanner.nextLine();
                    if (modifyName .equals("0")){
                        System.out.println(custom2.getName()+" 未修改");
                    }
                    System.out.println("姓别：");
                    String modifyGender = scanner.nextLine();
                    if (modifyGender .equals("0")){
                        System.out.println(custom2.getGender()+" 未修改");
                    }
                    System.out.println("请输入年龄:");
                    Integer modifyAge = scanner.nextInt();
                    if (modifyAge .equals(0)){
                        System.out.println(custom2.getAge()+" 未修改");
                    }
                    System.out.println("电话：");
                    String modifyPhone = scanner.nextLine();
                    if (modifyPhone .equals("0")){
                        System.out.println(custom2.getPhoneNumber()+" 未修改");
                    }
                    System.out.println("邮箱：");
                    String modifyEmail = scanner.nextLine();
                    if (modifyEmail .equals("0")){
                        System.out.println(custom2.getEmail()+" 未修改");
                    }
                    Custom custom1 = new Custom();
                    if (modifyName != null) {
                        custom1.setName(modifyName);
                    }
                    if (modifyGender != null) {
                        custom1.setGender(modifyGender);
                    }
                    if (modifyAge != 0) {
                        custom1.setAge(modifyAge);
                    }
                    if (modifyPhone != null) {
                        custom1.setPhoneNumber(modifyPhone);
                    }
                    if (modifyEmail != null) {
                        custom1.setEmail(modifyEmail);
                    }
                    modifyCustom(i1, custom1);
                    break;
                case '3':
                    //删除
                    System.out.println("请输入要删除的用户的id：");
                    Integer deleteId = scanner.nextInt();
                    removeCustom(deleteId);
                    break;
                case '4':
                    //获取所有
                    getAllCustom();
                    break;
                case '5':
                    //获取单个
//                    Scanner scanner1 = new Scanner(System.in);
                    System.out.println("请输入id查询：");
                    int i = scanner.nextInt();
                    getCustom(i);
                    break;
                case '6':
                    System.out.println("请确认(y/n)?");
                    boolean biaozhi = true;
                    while (biaozhi) {
//                        Scanner scanner = new Scanner(System.in);
                        String s = this.scanner.nextLine();
                        if (s.equals("y") || s.equals("Y")) {
                            biaozhi = false;
                            flag = false;
                            System.out.println("退出成功！");
                        } else if (s.equals("n") || s.equals("N")) {
                            biaozhi = false;
                            System.out.println("取消退出");
                        } else {
                            System.out.println("输入有误,请重新输入：");
                        }
                    }

                    break;
                default:
                    System.out.println("我也不知道");
                    return;
            }
        }

    }

    //添加
    private void addNewCustom(Custom custom) {
        boolean b = customList.addCustom(custom);
        if (b) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    //修改
    private void modifyCustom(Integer id, Custom custom) {
        boolean b = customList.modifyCustom(id, custom);
        if (b) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
    }

    //获取所有
    private void getAllCustom() {
        if (customList.getTotal() != 0) {
            Custom[] allCustom = customList.findAllCustom();
            System.out.println(Arrays.toString(allCustom) + "\t");
        } else {
            System.out.println("没有任何客户,请添加客户");
        }
    }

    //根据id获取
    private void getCustom(int i) {
        System.out.println(customList.findCustom(i));
    }

    //删除
    private void removeCustom(Integer id) {
        boolean b = customList.deleteCustom(id);
        if (b) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }

    //获取客户个数
    private void getCustomNum() {
        System.out.println(customList.getTotalNum());
    }


    public static void main(String[] args) {
        CustomView customView = new CustomView();
        customView.enterMainMenu();
    }


}
